//
//  CLNavViewController.m
//  chanlin
//
//  Created by Ryan on 14-12-18.
//  Copyright (c) 2014年 chanlin. All rights reserved.
//

#import "CLNavViewController.h"
#import "CLGloble.h"
#import "UIImageView+WebCache.h"
#import "CLLoginInViewController.h"
#import "CLPushAnimatedRight.h"
#import "UIView+Utils.h"
#import "SgrGCD.h"
#import "NSString+Filter.h"
#import "Sgr_macro.h"
#import "CLCollectViewController.h"
#import "BlockUI.h"
#import "CLPushAnimatedRight.h"
#import "CLPushViewController.h"
#import "CLSlidersViewController.h"
#import "CLSettingViewController.h"
#import "CLUserInfoViewController.h"


@interface CLNavViewController ()

@property (nonatomic,strong) UIImageView *backroundView;

@property (nonatomic,strong) UIView *userView;

@property (nonatomic,strong) UIImageView *icon;

@property (nonatomic,strong) UILabel *uname;
@property (nonatomic,strong) UILabel *text;
@property (nonatomic,strong) UILabel *auth;

@property (nonatomic,strong) UIButton *button1;
@property (nonatomic,strong) UIButton *button2;
@property (nonatomic,strong) UIButton *button3;
@property (nonatomic,strong) UIButton *button4;

@property (nonatomic,strong) UIImageView *chan;
@property (nonatomic,strong) NSArray *icons;

@end

@implementation CLNavViewController

- (instancetype)init{
    self=[super init];
    if(self){
        
    }
    return self;
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.backroundView=[[UIImageView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.backroundView];
    
    int y=(GlobleHeight==480)?41.f:85.f;
    
    self.userView=[[UIView alloc] init];
    self.userView.frame=CGRectMake(74.f, y, 80.f, 80.f);
    self.userView.layer.cornerRadius=40.f;
    self.userView.layer.borderWidth=0.5f;
    self.userView.layer.borderColor=RGB(0x8b, 0x85, 0x85).CGColor;
    
    self.icon=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 60.f, 60.f)];
    self.icon.layer.cornerRadius=30.f;
    self.icon.userInteractionEnabled=YES;
    self.icon.clipsToBounds=YES;
    [self.userView addSubview:self.icon];
    
    self.uname=[[UILabel alloc] init];
    self.uname.font=[UIFont systemFontOfSize:13];
    
    self.uname.frame=CGRectMake(74.f, self.userView.bottom+8.f, 80.f, self.uname.font.lineHeight);
    self.uname.textAlignment=NSTextAlignmentCenter;
    self.uname.textColor=RGB(0xe7, 0xe2, 0xdf);
    [self.view addSubview:self.uname];
    
    self.text=[[UILabel alloc] initWithFrame:CGRectMake(25, self.uname.bottom+22, 210.f, 0)];
    self.text.font=[UIFont systemFontOfSize:13.f];
    self.text.textColor=RGB(0xe7, 0xe2, 0xdf);
    self.text.numberOfLines=0;
    [self.view addSubview:self.text];
    
    self.auth=[[UILabel alloc] init];
    self.auth.font=[UIFont systemFontOfSize:12.f];
    self.auth.frame=CGRectMake(25.f, self.uname.bottom+8, 170, self.auth.font.lineHeight);
    self.auth.textColor=RGB(0x91, 0x87, 0x7d);
    self.auth.textAlignment=NSTextAlignmentRight;
    [self.view addSubview:self.auth];
    
    //self.box=[[UIImageView alloc] init];
    
    int y1=(GlobleHeight==568)?277:233;
    
    self.button1=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.button1 setImage:[UIImage imageNamed:@"nav_box.png"] forState:UIControlStateNormal];
    self.button1.frame=CGRectMake(25.f, GlobleHeight-y1, 90.f, 90.f);
    [self.view addSubview:self.button1];
    
    UIImageView *bimg=[UIImageView new];
    bimg.image=[UIImage imageNamed:@"nav_collect.png"];
    bimg.frame=CGRectMake(30.f, 15.f, 30.f, 30.f);
    [self.button1 addSubview:bimg];
    
    UILabel *btitle=[UILabel new];
    btitle.font=[UIFont systemFontOfSize:12.f];
    btitle.text=@"收藏";
    btitle.textAlignment=NSTextAlignmentCenter;
    btitle.frame=CGRectMake(15.f, 60.f, 60.f, btitle.font.lineHeight);
    btitle.textColor=RGB(0xf2, 0xe1, 0xcf);
    [self.button1 addSubview:btitle];
    
    [self.button1 handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        CLCollectViewController *collect=[CLCollectViewController new];
        [[CLPushAnimatedRight sharedInstance] pushController:collect];
    }];
    
    
    self.button2=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.button2 setImage:[UIImage imageNamed:@"nav_box.png"] forState:UIControlStateNormal];
    self.button2.frame=CGRectMake(25.f+89.5f, GlobleHeight-y1, 90.f, 90.f);
    [self.view addSubview:self.button2];
    
    bimg=[UIImageView new];
    bimg.image=[UIImage imageNamed:@"nav_push.png"];
    bimg.frame=CGRectMake(30.f, 15.f, 30.f, 30.f);
    [self.button2 addSubview:bimg];
    
    btitle=[UILabel new];
    btitle.font=[UIFont systemFontOfSize:12.f];
    btitle.text=@"推送历史";
    btitle.textAlignment=NSTextAlignmentCenter;
    btitle.frame=CGRectMake(15.f, 60.f, 60.f, btitle.font.lineHeight);
    btitle.textColor=RGB(0xf2, 0xe1, 0xcf);
    [self.button2 addSubview:btitle];
    [self.button2 handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        CLPushViewController *push=[CLPushViewController new];
        [[CLPushAnimatedRight sharedInstance] pushController:push];
    }];
    
    self.button3=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.button3 setImage:[UIImage imageNamed:@"nav_box.png"] forState:UIControlStateNormal];
    self.button3.frame=CGRectMake(25.f, GlobleHeight-y1+89.5f, 90.f, 90.f);
    [self.view addSubview:self.button3];
    
    bimg=[UIImageView new];
    bimg.image=[UIImage imageNamed:@"nav_feedback.png"];
    bimg.frame=CGRectMake(30.f, 15.f, 30.f, 30.f);
    [self.button3 addSubview:bimg];
    
    btitle=[UILabel new];
    btitle.font=[UIFont systemFontOfSize:12.f];
    btitle.text=@"联系我们";
    btitle.textAlignment=NSTextAlignmentCenter;
    btitle.frame=CGRectMake(15.f, 60.f, 60.f, btitle.font.lineHeight);
    btitle.textColor=RGB(0xf2, 0xe1, 0xcf);
    [self.button3 addSubview:btitle];
    
    [self.button3 handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate = self;
        
        [picker setSubject:@"联系我们"];
        NSArray *toRecipients = [NSArray arrayWithObject:@"chanlinapp@126.com"];
        
        
        [picker setToRecipients:toRecipients];
        if(picker)
        [self presentModalViewController:picker animated:YES];
//        CLSlidersViewController *slider=[CLSlidersViewController new];
//        slider.isCover=YES;
//        [[CLPushAnimatedRight sharedInstance] pushController:slider];
    }];

    
    self.button4=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.button4 setImage:[UIImage imageNamed:@"nav_box.png"] forState:UIControlStateNormal];
    self.button4.frame=CGRectMake(25.f+89.5f, GlobleHeight-y1+89.5, 90.f, 90.f);
    [self.view addSubview:self.button4];
    
    bimg=[UIImageView new];
    bimg.image=[UIImage imageNamed:@"nav_setting.png"];
    bimg.frame=CGRectMake(30.f, 15.f, 30.f, 30.f);
    [self.button4 addSubview:bimg];
    
    btitle=[UILabel new];
    btitle.font=[UIFont systemFontOfSize:12.f];
    btitle.text=@"设置";
    btitle.textAlignment=NSTextAlignmentCenter;
    btitle.frame=CGRectMake(15.f, 60.f, 60.f, btitle.font.lineHeight);
    btitle.textColor=RGB(0xf2, 0xe1, 0xcf);
    [self.button4 addSubview:btitle];
    
    [self.button4 handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        CLSettingViewController *setting=[CLSettingViewController new];
        [[CLPushAnimatedRight sharedInstance] pushController:setting];
    }];

    
//    self.box.frame=CGRectMake(25.f, GlobleHeight-y1, 180.f, 180.f);
//    self.box.userInteractionEnabled=YES;
//    self.box.image=[UIImage imageNamed:@"nav_box.png"];
//    [self.view addSubview:self.box];
//    if(_isStrNotNull([CLGloble sharedInstance].user.imgUrl)){
//        [self.icon sd_setImageWithURL:
//         [NSURL URLWithString:[CLGloble sharedInstance].user.imgUrl]
//                     placeholderImage:nil];
//    }else{
//        self.icon.image=[UIImage imageNamed:@"headIcon.png"];
//    }
    
    [self.view addSubview:self.userView];
    
    self.chan=[[UIImageView alloc] initWithFrame:CGRectMake(self.icon.width-18, self.icon.height-18, 18, 18)];
    self.icons=@[@"groupId_1_b.png",@"groupId_2_b.png",@"groupId_3_b.png"];
    //self.chan.image=[UIImage imageNamed:@"nav_about.png"];
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(login)];
    [self.icon addGestureRecognizer:tap];
    
    [self addBackgroundView];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(addBackgroundView)
                                                 name:UIApplicationDidBecomeActiveNotification
                                               object:nil];
}

- (void)viewWillAppear:(BOOL)animated{
    if(_isStrNotNull([CLGloble sharedInstance].user.imgUrl)){
        [self.icon sd_setImageWithURL:
         [NSURL URLWithString:[CLGloble sharedInstance].user.imgUrl]
                     placeholderImage:[UIImage imageNamed:@"headIcon.png"]];
        
    }else{
        self.icon.image=[UIImage imageNamed:@"headIcon.png"];
        
    }
    
    if(_isStrNotNull([CLGloble sharedInstance].user.nickName)){
        self.uname.text=[CLGloble sharedInstance].user.nickName;
    }else{
        self.uname.text=@"请修改昵称";
    }
    
    if(_isStrNULL([CLGloble sharedInstance].user.token)){
        self.uname.text=@"登录";
    }
    
   
    int index=[[CLGloble sharedInstance].user.groupId intValue]-2;
    
    if(index>=0){
        CGRect frame=[self.icon convertRect:CGRectMake(self.icon.width-16, self.icon.height-16, 18, 18) toView:self.view];
        self.chan.frame=frame;
        [self.view addSubview:self.chan];
        
        self.chan.image=[UIImage imageNamed:[self.icons sgrGetStringForIndex:index]];
    }else{
        [self.chan removeFromSuperview];
    }
    
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    [controller dismissViewControllerAnimated:YES completion:nil];
    if(result==MFMailComposeResultFailed){
        [[CLGloble sharedInstance] showTieAlertMessage:@"邮箱打开失败" subMessage:@"请检查邮箱配置!"];
    }
}


- (NSArray *)showText{
    int index=(arc4random()%4);
    NSString *path=[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"navtext%d",index]
                                                   ofType:@"txt"];
    NSString *string=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    string=[string trim];
    if(_isStrNULL(string)) return nil;
    NSMutableArray *array=[NSMutableArray array];
    __block NSMutableArray *lines=[NSMutableArray array];
    [string enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
        if([[line trim] length]==0){
            [array sgrAddObject:lines];
            lines=[NSMutableArray array];
        }else{
            [lines sgrAddObject:line];
        }
    }];
    if([array count]==0) return nil;
    
     index=(arc4random()%[array count]);
    return [array sgrGetarrayForIndex:index];
    
}

- (void)login{
    if(_isStrNULL([CLGloble sharedInstance].user.token)){
        CLLoginInViewController *login=[[CLLoginInViewController alloc] init];
        [[CLPushAnimatedRight sharedInstance] pushController:login];
    }else{
        CLUserInfoViewController *user=[CLUserInfoViewController new];
        [[CLPushAnimatedRight sharedInstance] pushController:user];
    }
}

- (void)addBackgroundView{
   
    
    int index=(arc4random()%3)+1;
    self.backroundView.image=[UIImage imageNamed:[NSString stringWithFormat:@"nav_bg%d.png",index]];
    __weak typeof(self)me=self;
    [[SgrGCD sharedInstance] enqueueGloble:^{
        NSArray *array=nil;
        double last=[[NSUserDefaults standardUserDefaults] doubleForKey:@"navText_time"];
        if((fabs([[NSDate date] timeIntervalSince1970]-last))>(3600*24)){
            array=[me showText];
            [[NSUserDefaults standardUserDefaults] setObject:array forKey:@"navText"];
            double time=[[NSDate date] timeIntervalSince1970];
            [[NSUserDefaults standardUserDefaults] setDouble:time forKey:@"navText_time"];
          
        }else{
            array=[[NSUserDefaults standardUserDefaults] arrayForKey:@"navText"];
        }
        
     
        
        
        sgrSafeMainThread(^{
            if(array && [array count]>1){
                me.text.hidden=NO;
                me.auth.hidden=NO;
                me.text.height=self.text.font.lineHeight*([array count]-1);
                
                me.auth.top=me.text.bottom+8.f;
                NSMutableString *string=[[NSMutableString alloc] init];
                for(int i=0;i<[array count]-1;i++){
                    [string appendString:[array sgrGetStringForIndex:i]];
                    if(i!=([array count]-2)){
                        [string appendString:@"\n"];
                    }
                }
             
                me.text.text=string;
                me.auth.text=[array sgrGetStringForIndex:[array count]-1];
                
            }else{
                me.text.hidden=YES;
                me.auth.hidden=YES;
            }
        });
    }];
}

- (BOOL)showPopAction{
    return YES;
}


@end
